var express = require('express');
var {userModel} = require('../database/model/stuModel')
var router = express.Router();

//用户注册
router.post('/', function(req, res, next) {
  console.log(req.body);
  //判断账号是否可用
  let {username,password} = req.body
  userModel.find({username}).then(result=>{
    if(result.length){
      res.send({
        code:0,
        msg:'账号已被占用'
      })
      return
    }
    new userModel({
      username,
      password
    }).save().then(result=>{
      res.send({
        code:1,
        msg:'注册成功'
      })
    })
  })
});


/* 用户登录 */
router.get('/',function(req,res,next) {
  console.log(req.query);
  let {username,password} = req.query
  userModel.find({username}).then(result=>{
    if(!result.length){
      res.send({
        code:0,
        msg:'账号不存在，请注册'
      })
      return
    }
    if(result[0].password!=password){
      res.send({
        code:0,
        msg:'密码错误'
      })
      return
    }
    res.send({
      code:1,
      msg:'登录成功',
      data:result[0]
    })
  })
})

module.exports = router;
